package com.jushu.storbox.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.jushu.storbox.entities.ImageBean;
import com.jushu.storbox.util.image.NativeImageLoader;
import com.jushu.storbox.util.image.NativeImageLoader.NativeImageCallBack;
import com.jushu.storbox.view.viewImpl.MyImageView;
import com.jushu.storbox.view.viewImpl.MyImageView.OnMeasureListener;


public class FileBrowserPictureAdapter extends BaseAdapter
{
    private List<ImageBean> list;
    
    private Point mPoint = new Point(0, 0);
    
    private GridView mGridView;
    
    protected LayoutInflater mInflater;
    @Override
    public int getCount()
    {
        return list.size();
    }
    
    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }
    
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    public FileBrowserPictureAdapter(Context context, List<ImageBean> list, GridView mGridView)
    {
        this.list = list;
        this.mGridView = mGridView;
        mInflater = LayoutInflater.from(context);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final ViewHolder viewHolder;
        ImageBean mImageBean = list.get(position);
        String path = mImageBean.getTopImagePath();
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.grid_group_item, null);
            viewHolder.layout_camera = convertView.findViewById(R.id.layout_camera);
            viewHolder.framelayout = convertView.findViewById(R.id.framelayout);
            viewHolder.mImageView = (MyImageView) convertView.findViewById(R.id.group_image);
            viewHolder.mTextViewTitle = (TextView) convertView.findViewById(R.id.group_title);
            viewHolder.mTextViewCounts = (TextView) convertView.findViewById(R.id.group_count);
                viewHolder.mImageView.setOnMeasureListener(new OnMeasureListener()
                {
                    @Override
                    public void onMeasureSize(int width, int height)
                    {
                        mPoint.set(width, height);
                    }
                });
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.mImageView.setImageResource(R.mipmap.empty_pic);
        }
        
        if(position != 0)
        {
        	viewHolder.layout_camera.setVisibility(View.GONE);
        	viewHolder.framelayout.setVisibility(View.VISIBLE);
        	viewHolder.mTextViewTitle.setText(mImageBean.getFolderName());
            viewHolder.mTextViewCounts.setText(Integer.toString(mImageBean.getImageCounts()));
            viewHolder.mImageView.setTag(path);
            
            Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(path,
                mPoint,
                new NativeImageCallBack()
                {
                    
                    @Override
                    public void onImageLoader(Bitmap bitmap, String path)
                    {
                        ImageView mImageView = (ImageView) mGridView.findViewWithTag(path);
                        if (bitmap != null && mImageView != null)
                        {
                            mImageView.setImageBitmap(bitmap);
                        }
                    }
                });
            
            if (bitmap != null)
            {
                viewHolder.mImageView.setImageBitmap(bitmap);
            }
            else
            {
                viewHolder.mImageView.setImageResource(R.mipmap.empty_pic);
            }
        }
        else
        {
        	viewHolder.layout_camera.setVisibility(View.VISIBLE);
        	viewHolder.framelayout.setVisibility(View.GONE);
        }
        return convertView;
    }
    
    public static class ViewHolder
    {
    	public View layout_camera,framelayout;
        public MyImageView mImageView;
        
        public TextView mTextViewTitle;
        
        public TextView mTextViewCounts;
    }
    
}
